package com.zty.advice;

import com.zty.errors.BusinessException;
import com.zty.response.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.Arrays;

/**
 * @Author ZTY
 * @Description 配置全局异常处理
 * @Date 0:26 2023-12-23
 **/

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handleException(BusinessException exception) {
        exception.printStackTrace();
        log.info(exception.getMessage());
        log.error(formatExceptionMsg(exception.getMessage(), exception));
        return CommonResponse.create(500, "", exception.getMessage());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handleException(MethodArgumentNotValidException exception)
    {
        FieldError fieldError =
                exception.getBindingResult().getFieldError();
        assert fieldError != null;
        log.error(formatExceptionMsg(fieldError.getField() + " : " +
                fieldError.getDefaultMessage(), exception));
        return CommonResponse.create(500, "",
                fieldError.getDefaultMessage());
    }
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handlerRuntimeException(RuntimeException ex) {
        ex.printStackTrace();
        return CommonResponse.create(500, null, "请求出现异常");
    }
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handlerException(Exception ex) {
        ex.printStackTrace();
        return CommonResponse.create(500, new Object(), "请求出现异常");
    }
    public static String formatExceptionMsg(String errMsg, Throwable e) {
        StackTraceElement[] stackTrace = e.getStackTrace();
        String stackTraceString = Arrays.toString(stackTrace).replace("[",
                "").replace("]", "").replace(",", "\n\t");
        return MessageFormat.format(
                "异常标题: {0} \n" +
                        "文件名: {1} \n" +
                        "类名: {2}\n" +
                        "方法名: {3}\n " +
                        "抛出异常行号: {4}\n " +
                        "异常栈信息: {5}\n ",
                errMsg,
                e.getStackTrace()[0].getFileName(),
                e.getStackTrace()[0].getClassName(),
                e.getStackTrace()[0].getMethodName(),
                e.getStackTrace()[0].getLineNumber(),
                stackTraceString
        );
    }
}
